package com.elinshaw.hadoopdev;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * /*
 * 指定Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>
 * 这4个类型分别为LongWritable, Text, Text, IntWritable，
 * 相当于普通类型long，string，string，int
 * setup 和 clean 方法不是必须的
 *
 * @author Administrator
 */
public class TestMapper extends Mapper<LongWritable, Text, Text, LongWritable> {

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        //最先执行
        System.out.println("TestMapper is setup");
    }

    @Override
    public void run(Context context) throws IOException, InterruptedException {
        System.out.println("TestMapper is run");
        super.run(context);
    }

    /**
     * 业务逻辑要处理的数据已经被框架传递进来在方法的参数中key-value
     * key是这一行数据的起始偏移量，这一行内容作为value
     * 输入key和value，map的结果写入到context中
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] str = value.toString().split(",");
        for (String s : str) {
            context.write(new Text(s), new LongWritable(1));
        }
    }

    @Override
    protected void cleanup(Context context) throws IOException, InterruptedException {
        //执行完map之后执行
        System.out.println("TestMapper is cleanup");
    }
}
